//
//  RVRentCarListController.swift
//  AirTravel
//
//  Created by 许必成 on 2021/11/27.
//

import UIKit
import HandyJSON
class RVRentCarListController: CommonVC {
    
    @IBOutlet weak var tableView: UITableView!
    
    var dataSource: [RVRentCarList] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        head.title.text = "租车订单"
        confTableView()
        getDatas()
    }

    func confTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "RVRentCarListCell", bundle: nil), forCellReuseIdentifier: "cell1")
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 200
    }
    
    func getDatas(){
        YProgressHUD.show()
        NetWorkRequest(API1.getRentCarList) { json in
            
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVRentCarData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            self.dataSource = data.data
            self.tableView.reloadData()
            
        } failed: { msg in
            print("=====")
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
            print("=====")

        }

    }

   
}

extension RVRentCarListController: UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! RVRentCarListCell
        let list = dataSource[indexPath.row]
        cell.nameLabel.text = "\(list.contact)，\(list.mobile)"
        cell.statusLabel.text = "提交成功"
        cell.timeLabel.text = list.create_time
        cell.modelLabel.text = list.model
        cell.addressLabel.text = list.address
        
        if list.desc.count == 0 {
            cell.remarkLabel.text = "备注：暂无"
        }else{
            cell.remarkLabel.text = "备注：\(list.desc)"
        }
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

